package view
{
	import modules.simplemodule.SimpleModule;	
	
	import mx.events.FlexEvent;			import common.HelloWorldConstants;
	
	import flash.display.DisplayObject;
	import flash.utils.getQualifiedClassName;
	
	import mx.events.ModuleEvent;
	import mx.utils.UIDUtil;
	
	import org.osflash.thunderbolt.Logger;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.components.FlexModuleLoader;
	import org.puremvc.as3.multicore.utilities.fabrication.events.FabricatorEvent;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.mediator.FlexMediator;
	
	import view.components.ModuleTile;

	public class ModuleTileMediator extends FlexMediator
	{
		public static const NAME:String = getQualifiedClassName(ModuleTileMediator);
		public var moduleLoader:FlexModuleLoader;
		
		public function ModuleTileMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			super.onRegister();
			registerEventListeners();
		}
		
		
		private function registerEventListeners():void
		{
			
		}
		
		public function respondToAddModule(note:INotification):void
		{
			Logger.info(NAME+ " : respondToAddModule()");
			
			addModule();
		}
		
		private function addModule():void
		{
			moduleLoader = new FlexModuleLoader();
			
			moduleLoader.id = "SimpleModule : "+ UIDUtil.createUID();
			moduleLoader.url = "modules/simplemodule/SimpleModule.swf";
			moduleLoader.router = applicationRouter;
			
			moduleLoader.addEventListener(ModuleEvent.ERROR, moduleError);
			moduleLoader.addEventListener(FabricatorEvent.FABRICATION_CREATED, moduleCreated);
			moduleLoader.addEventListener(FabricatorEvent.FABRICATION_REMOVED, moduleRemoved);
			moduleLoader.addEventListener(ModuleEvent.READY, moduleReady);
			
			moduleLoader.loadModule();
		}
		
		private function moduleError(e:ModuleEvent):void
		{
			Logger.error(NAME + " : moduleError()");	
		}
		
		private function moduleCreated(e:FabricatorEvent):void
		{
			var moduleLoader:FlexModuleLoader = e.target as FlexModuleLoader;
			moduleLoader.removeEventListener(FabricatorEvent.FABRICATION_CREATED, moduleCreated);
			
			Logger.info(NAME+ " : moduleCreated() moduleAddress = " + fabrication.moduleAddress.getClassName());
			routeNotification(HelloWorldConstants.SAY_HELLO_MESSAGE, HelloWorldConstants.SAY_HELLO_MESSAGE, null, "SimpleModule/*");
			Logger.info(NAME+ " : moduleCreated()");
		}
		
		private function moduleRemoved(e:FabricatorEvent):void
		{
			var moduleLoader : FlexModuleLoader = e.target as FlexModuleLoader;
			moduleLoader.removeEventListener(FabricatorEvent.FABRICATION_REMOVED, moduleRemoved);
			
			Logger.info(NAME+ " : moduleRemoved()");	
		}
		
		private function moduleReady(e:ModuleEvent):void
		{
			e.target.removeEventListener(ModuleEvent.READY, moduleReady);
			app.moduleTileList.addChild(e.target as DisplayObject);

		}
		
		public function get app():ModuleTile
		{
			return viewComponent as ModuleTile;
		}
	}
}